package com.iqiyi.pps.epg.core.dao.stream;

import com.iqiyi.pps.epg.core.dao.base.BaseDao;
import com.iqiyi.pps.epg.core.model.stream.LiveStream;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2014/12/9.
 */
@Component
public class LiveStreamBaseDao extends BaseDao<LiveStream> {

    private static final String QUERY_LIVESTREAM_BY_FATHERID = "select l from LiveStream l where l.fatherId=? and l.status<> -1";
    private static final String QUERY_LIVESTREAM_BY_FATHERID_STREAMNAME = "select l from LiveStream l where l.fatherId=? and l.streamName=? and l.status<> -1";
    private static final String QUERY_LIVESTREAM_BY_FATHERID_STREAMNAME_FORMATTYPE = "select l from LiveStream l where l.fatherId=? and l.streamName=? and l.formatType=? and l.status<> -1";
    private static final String QUERY_LIVESTREAM_BY_FATHERID_FORMATTYPE_ISDEFAULT = "select l from LiveStream l where l.fatherId=? and l.formatType=? and l.isDefault=? and l.status<> -1";
    private static final String QUERY_LIVESTREAM_BY_FATHERID_STREAMTYPE = "select l from LiveStream l where l.fatherId=? and l.formatType=? and l.status<> -1";
    private static final String  QUERY_LIVESTREAM_BY_STATUS = "select l from LiveStream l where l.fatherId=? and l.streamName=? and l.status=?";

    public List<LiveStream> get(Long fatherId, String streamName) {
        if (StringUtils.isNotEmpty(streamName)) {
            List<Object> list = new ArrayList<Object>();
            list.add(fatherId);
            list.add(streamName);
            return createQuery(QUERY_LIVESTREAM_BY_FATHERID_STREAMNAME, list).list();
        } else {
            return createQuery(QUERY_LIVESTREAM_BY_FATHERID, fatherId).list();
        }
    }

    public LiveStream get(Long fatherId, String streamName, String formatType) {
        List<Object> list = new ArrayList<Object>();
        list.add(fatherId);
        list.add(streamName);
        list.add(formatType);
        List<LiveStream> liveStreams = createQuery(QUERY_LIVESTREAM_BY_FATHERID_STREAMNAME_FORMATTYPE, list).list();
        int size = liveStreams.size();
        if (size == 0) {
            return null;
        } else {
            return liveStreams.get(0);
        }
    }

    public LiveStream getByDefault(Long fatherId, String formatType, Boolean isDefault) {
        List<Object> list = new ArrayList<Object>();
        list.add(fatherId);
        list.add(formatType);
        list.add(isDefault);
        List<LiveStream> liveStreams = createQuery(QUERY_LIVESTREAM_BY_FATHERID_FORMATTYPE_ISDEFAULT, list).list();
        int size = liveStreams.size();
        if (size == 0) {
            return null;
        } else {
            return liveStreams.get(0);
        }
    }

    public List<LiveStream> getByFormatType(Long fatherId, String formatType) {
        List<Object> list = new ArrayList<Object>();
        list.add(fatherId);
        list.add(formatType);
        List<LiveStream> liveStreams = createQuery(QUERY_LIVESTREAM_BY_FATHERID_STREAMTYPE, list).list();
        return liveStreams;
    }

    public List<LiveStream> getByStatus(Long fatherId, String streamName, int status) {
        List<Object> list = new ArrayList<Object>();
        list.add(fatherId);
        list.add(streamName);
        list.add(status);
        List<LiveStream> liveStreams = createQuery(QUERY_LIVESTREAM_BY_STATUS, list).list();
        return liveStreams;
    }

    public void delete(Long fatherId, String streamName, String formatType) {
        LiveStream liveStream = get(fatherId, streamName, formatType);
        if (liveStream != null) {
            liveStream.setStatus(-1);
            liveStream.setIsDefault(false);
            save(liveStream);
        }
    }

    @Override
    public void delete(Long id) {
        LiveStream liveStream = get(id);
        if (liveStream != null) {
            liveStream.setStatus(-1);
            save(liveStream);
        }
    }
}
